﻿using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using TA.JSON;

namespace TA.Turbo
{
	/// <summary>
	/// Describes styles used to create a specially styled element.
	/// </summary>
	[TypeConverter(typeof(ExpandableObjectConverter))]
	public class StyledElement : IJsonCompatible
	{
		#region PUBLIC
		#region INIT
		/// <summary>
		/// Ctor.
		/// </summary>
		public StyledElement()
		{
			this.Visible = true;
			this.CssClass = "";
			this.Style = "";
		}
		#endregion
		#region PROPS
		/// <summary>
		/// CSS class applied to the element.
		/// </summary>
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string CssClass { get; set; }
		/// <summary>
		/// CSS styles applied to the element.
		/// </summary>
		[DefaultValue("")]
		[NotifyParentProperty(true)]
		public string Style { get; set; }
		/// <summary>
		/// Specifies whether this element should be visible.
		/// </summary>
		[DefaultValue(true)]
		[NotifyParentProperty(true)]
		public bool Visible { get; set; }
		#endregion
		#endregion
		#region INTERNAL
		#region JSON
		/// <summary>
		/// Gets true if any of the property values are non-default.
		/// </summary>
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[Browsable(false)]
		public virtual bool IsDirty
		{
			get
			{
				return
					!string.IsNullOrEmpty(this.CssClass) ||
					!string.IsNullOrEmpty(this.Style) ||
					!this.Visible;
			}
		}
		/// <summary>
		/// Serializes this object.
		/// </summary>
		/// <param name="jw"></param>
		public void Serialize(JsonWriter jw)
		{
			this.SerializeObject(jw);
		}
		/// <summary>
		/// SerializeContents.
		/// </summary>
		/// <param name="jw"></param>
		public virtual void SerializeContents(JsonWriter jw)
		{
			if (!string.IsNullOrEmpty(this.CssClass))
			{
				jw.PropertyName("C");
				jw.Value(this.CssClass);
			}

			if (!string.IsNullOrEmpty(this.Style))
			{
				jw.PropertyName("S");
				jw.Value(this.Style);
			}

			if (!this.Visible)
			{
				jw.PropertyName("V");
				jw.Value(this.Visible);
			}
		}
		/// <summary>
		/// Deserializes this object.
		/// </summary>
		/// <param name="jr"></param>
		public void Deserialize(IJsonReader jr)
		{
			this.DeserializeObject(jr);
		}
		/// <summary>
		/// DeserializeContent.
		/// </summary>
		/// <param name="jr"></param>
		/// <param name="propShortName"></param>
		public virtual void DeserializeContent(IJsonReader jr, string propShortName)
		{
			switch (propShortName)
			{
				case "C": this.CssClass = (string)jr.Value; break;
				case "S": this.Style = (string)jr.Value; break;
				case "V": this.Visible = (bool)jr.Value; break;
			}
		}
		#endregion
		#endregion
	}
}